package com.spring.boot.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.spring.boot.core.cache.redis.RedisService;
import com.spring.boot.core.lock.RLock;
import com.spring.boot.dao.mapper.HotelMapper;
import com.spring.boot.dao.mapper.PublicMapper;
import com.spring.boot.dao.model.Hotel;
import com.spring.boot.dao.model.UserInfoDO;
import com.spring.boot.entity.Student;
import com.spring.boot.service.StudentService;

/**
 * Created by 三劫散仙 on 2015/4/24.
 */
@Controller
@RequestMapping("/stu")
@EnableAutoConfiguration
public class StudentController {

	private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

	@Autowired
	private StudentService studentService;

	@Autowired
	private HotelMapper hotleMapper;

	@Autowired
	private PublicMapper publicMapper;

	@Autowired
	private RedisService redis;

	@RequestMapping("/test/{id}")
	@ResponseBody
	@Cacheable(value = "student", keyGenerator = "wiselyKeyGenerator")
	public Hotel test(@PathVariable int id) {
		Hotel hotel = hotleMapper.selectByCityId(id);
		return hotel;
	}

	@RequestMapping("/test1")
	@ResponseBody
	public List<UserInfoDO> test1() {
		// 分布式锁
		RLock lock = redis.getLock("lock");
		lock.lock();
		redis.set("name", "testname");
		// 逻辑
		lock.uncLock();
		PageHelper.startPage(1, 1);
		logger.info("redis.............." + redis.get("name"));
		return publicMapper.getAllUserInfo();
	}

	@RequestMapping(value = "/{id}")
	@ResponseBody
	public Student getStudent(@PathVariable int id) {
		logger.info("获取单个学生信息");
		return studentService.getStudentById(id);
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public List<Student> getAllStudents() {
		logger.info("获取所有学生信息");
		return studentService.getAllStudents();
	}

	@RequestMapping(value = "/add/{name}/{age}")
	@ResponseBody
	public List<Student> addStudent(@PathVariable String name, @PathVariable int age) {
		logger.info("增加学生信息,返回所有的列表");
		studentService.addStudent(name, age);
		return studentService.getAllStudents();
	}

	@RequestMapping(value = "/del/{id}")
	@ResponseBody
	public List<Student> delStudent(@PathVariable int id) {
		logger.info("删除学生信息");
		studentService.delStudent(id);
		return studentService.getAllStudents();
	}

	@RequestMapping(value = "/add1/{name}/{age}")
	@ResponseBody
	public Student addStudentAndReturn(@PathVariable String name, @PathVariable int age) {
		logger.info("增加学生信息,返回当前添加的");
		int id = studentService.addStudentAndReturn(name, age);
		return studentService.getStudentById(id);
	}
}